/**
 * @author: hhj
 * @date: 2022年10月6日 下午3:56:58
 */
package com.syc.test;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: hhj
 * @date: 2022年10月6日 下午3:56:58
 */
@Slf4j
public class HtmlUtil {


    public static List<String> getVideos(String htmlStr) {
        List<String> videos = new ArrayList<>();
        if (StringUtils.isEmpty(htmlStr)) {
            return videos;
        }
        try {
            Document document = Jsoup.parse(htmlStr);
            Elements videoTags = document.getElementsByTag("video");
            if (!CollectionUtils.isEmpty(videoTags)) {
                for (Element videoTag : videoTags) {
                    String videoSrc = videoTag.attr("src");
                    if (StringUtils.isNotEmpty(videoSrc)) {
                        videos.add(videoSrc);
                    }
                }
            }
        } catch (Exception e) {
            log.error("解析视频异常", e);
        }
        return videos;
    }

    public static List<String> getPics(String htmlStr) {
        List<String> pics = new ArrayList<>();
        if (StringUtils.isEmpty(htmlStr)) {
            return pics;
        }
        try {
            Document document = Jsoup.parse(htmlStr);
            Elements imgs = document.getElementsByTag("img");
            if (!CollectionUtils.isEmpty(imgs)) {
                for (Element img : imgs) {
                    String imgSrc = img.attr("data-original");
                    if (StringUtils.isEmpty(imgSrc)) {
                        imgSrc = img.attr("src");
                    }
                    if (StringUtils.isNotEmpty(imgSrc)) {
                        if (imgSrc.startsWith("//")) {
                            imgSrc = "https:" + imgSrc;
                        }
                        pics.add(imgSrc);
                    }
                }
            }
        } catch (Exception e) {
            log.error("解析图片异常", e);
        }
        return pics;
    }

    public static String splitAndFilterStringNoSuffix(String input, int length) {
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        Document document = Jsoup.parse(input);
        if (document != null) {
            String text = document.text();
            if (StringUtils.isNotEmpty(text)) {
                if (text.length() > length) {
                    return text.substring(0, length).replaceAll("\r|\n", "");
                } else {
                    return text.replaceAll("\r|\n", "");
                }
            }
        }
        return "";
    }


    public static String imgFilter(String htmlStr) {
        if (StringUtils.isEmpty(htmlStr)) {
            return htmlStr;
        }
        try {
            Document document = Jsoup.parse(htmlStr);
            Elements imgs = document.getElementsByTag("img");
            if (!CollectionUtils.isEmpty(imgs)) {
                for (Element img : imgs) {
                    String imgSrc = img.attr("data-original");
                    if (StringUtils.isEmpty(imgSrc)) {
                        imgSrc = img.attr("src");
                        if (StrUtil.isNotEmpty(imgSrc) && imgSrc.contains("sina")) {
                            imgSrc = "https://hg.izhonghong.com/imageFilter?url=" + imgSrc;
                            img.attr("src", imgSrc);
                        }
                    } else if (imgSrc.contains("sina")) {
                        imgSrc = "https://hg.izhonghong.com/imageFilter?url=" + imgSrc;
                        img.attr("data-original", imgSrc);
                    }
                }
                return document.html();
            }
        } catch (Exception e) {
            log.error("图片处理异常", e);
        }
        return htmlStr;
    }


    public static String removeBlankLine(String htmlStr) {
        if (StringUtils.isEmpty(htmlStr)) {
            return htmlStr;
        }
        try {
            Document document = Jsoup.parse(htmlStr);
            Elements pTags = document.getElementsByTag("p");
            for (Element p : pTags) {
                if (StringUtils.isEmpty(p.html().trim())) {
                    p.remove();
                }
            }
            return document.html();
        } catch (Exception e) {
            log.error("去除空行异常", e);
        }
        return htmlStr;
    }


    public static Map<String, String> matchReplaceImg(String text) {
        Map<String, String> imgMap = new HashMap<>();
        Pattern ptn = Pattern.compile("<img.*?src=.*?>");
        Matcher matcher = ptn.matcher(text);

        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String imgTag = matcher.group();
            String randomKey = String.format("[%s_%s]", System.currentTimeMillis(), (int) (Math.random() * 100000));
            imgMap.put(randomKey, imgTag);

            matcher.appendReplacement(sb, randomKey);
        }
        matcher.appendTail(sb);

        imgMap.put("text", sb.toString());
        return imgMap;
    }
}
